import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ObjCreate } from "../../../common/ObjCreate";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { MsgManager } from "../../../../manager/MsgManager";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { EPlayerItemPosType } from "../../../../../../Message/Public/EPlayerItemPosType";
import { ItemData } from "../../../../resource/info/ItemData";
import { EColor } from "../../../../../../Message/Public/EColor";
import { TItemWeapon } from "../../../../../../Message/Db/Tables/TItemWeapon";
import { SuitConfig } from "../../../../resource/SuitConfig";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { _EBindAfterUse } from "../../../../../../EBind/_EBindAfterUse";
type int = number;
//class EquipMergePanel
    
    export  class EquipMergePanel extends BaseDeployPanel
    {
        constructor()
        {
            this._currentMaterialEquips = [];
            this.posData = [{"x": 100, "y": 105, "label": Language.getString(60184), "canRemove": true, "noBoarder": true}, {"x": 208, "y": 105, "label": Language.getString(60184), "canRemove": true, "noBoarder": true}, {"x": 156, "y": 150, "label": Language.getString(60185), "canRemove": true, "noBoarder": true}, {"x": 156, "y": 245, "label": Language.getString(60186), "canRemove": false, "noBoarder": false}];
            this.buttonLabel = Language.getString(60187);
            this._hintText1 = Language.getString(60188);
            this._alarmText = Language.getString(60189);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc4=null;
            var loc5=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(60190), 0, 66, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            var loc1=new GImageBitmap("EquipMerge_arrow.swf", 118, 127);
            this.addChild(loc1);
            loc2 = new GImageBitmap("equipment_successRate.png");
            loc2.x = 14;
            loc2.y = 98;
            successRateItem.addChild(loc2);
            successRateLabel.x = 33;
            successRateLabel.y = 122;
            addChild(successRateItem);
            var loc3=0;
            while (loc3 < this.posData.length) 
            {
                loc4 = this.posData[loc3];
                (loc5 = new DeployPanelItem(loc3, loc4.noBoarder)).posIndex = loc3;
                loc5.x = loc4["x"];
                loc5.y = loc4["y"];
                loc5.label = loc4["label"];
                if (loc4.canRemove) 
                {
                    loc5.mouseEnabled = true;
                    loc5.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc5);
                allItem.push(loc5);
                ++loc3;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!this.checkCanMerge()) 
            {
                return;
            }
            var loc1=arg1.target;
            switch (loc1) 
            {
                case confirmButton:
                {
                    this.onConfirm();
                    break;
                }
            }
            return;
        }

        /* internal  */checkCanMerge(): Boolean
        {
            if (!allItem[0].itemData || !allItem[1].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60191));
                return false;
            }
            if (!allItem[2].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60192));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */onConfirm(): void
        {
            var loc1=new Array<any>();
            loc1.push(allItem[2].itemData.uid);
            loc1.push(allItem[0].itemData.uid);
            loc1.push(allItem[1].itemData.uid);
            var loc2=new EquipUpdateParams();
            loc2.operationId = this._operationId;
            loc2.autoUpdate = false;
            loc2.equipPosType = EPlayerItemPosType._EPlayerItemPosTypeBag;
            loc2.materials = loc1;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc2, false, true));
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            if (allItem[0].itemData && allItem[1].itemData && allItem[2].itemData) 
            {
                loc1 = 2000;
                successRate = 100;
                this.showResultEquip();
            }
            else 
            {
                allItem[3].itemData = null;
                loc1 = 0;
                if (allItem[2].itemData && (!allItem[0].itemData || !allItem[1].itemData)) 
                {
                    this.moveOutByIndex(2);
                }
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=0;
            while (loc1 < 3) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.uid == arg1.uid) 
                {
                    this.moveOutByIndex(loc1);
                    return;
                }
                ++loc1;
            }
            if (arg1.itemInfo.item.color >= EColor._EColorPurple) 
            {
                if (!allItem[0].itemData && !allItem[1].itemData) 
                {
                    this._lastPutinIndex = 0;
                    allItem[0].itemData = arg1;
                    this._currentMaterialEquips.push(arg1);
                }
                else if (!allItem[0].itemData && allItem[1].itemData) 
                {
                    if (this.isInSameSuit(allItem[1].itemData, arg1)) 
                    {
                        this._lastPutinIndex = 0;
                        allItem[0].itemData = arg1;
                        this._currentMaterialEquips.push(arg1);
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(this._hintText1);
                        return;
                    }
                }
                else if (allItem[0].itemData && !allItem[1].itemData) 
                {
                    if (this.isInSameSuit(allItem[0].itemData, arg1)) 
                    {
                        this._lastPutinIndex = 1;
                        allItem[1].itemData = arg1;
                        this._currentMaterialEquips.push(arg1);
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(this._hintText1);
                        return;
                    }
                }
                else if (this.isInSameSuit(allItem[0].itemData, arg1)) 
                {
                    this._currentMaterialEquips.splice(this._currentMaterialEquips.indexOf(allItem[this._lastPutinIndex].itemData), 1);
                    this._lastPutinIndex = this._lastPutinIndex != 1 ? 1 : 0;
                    allItem[this._lastPutinIndex].itemData = arg1;
                    this._currentMaterialEquips.push(arg1);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(this._hintText1);
                    return;
                }
            }
            else if (arg1.itemInfo.item.color == EColor._EColorBlue) 
            {
                if (!allItem[0].itemData || !allItem[1].itemData) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60193));
                    return;
                }
                if (arg1.itemInfo.item.career != allItem[0].itemData.itemInfo.item.career) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60194));
                    return;
                }
                if (!this.checkIsInThisSuit((allItem[0].itemData.itemInfo.extend as TItemWeapon).suit, arg1.itemInfo.item.type)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60195));
                    return;
                }
                if (allItem[2].itemData) 
                {
                    this._currentMaterialEquips.splice(this._currentMaterialEquips.indexOf(allItem[2].itemData), 1);
                }
                allItem[2].itemData = arg1;
                this._currentMaterialEquips.push(arg1);
            }
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this._currentMaterialEquips, false, true));
            return;
        }

        /* internal  */isInSameSuit(arg1: ItemData, arg2: ItemData): Boolean
        {
            return (arg1.itemInfo.extend as TItemWeapon).suit == (arg2.itemInfo.extend as TItemWeapon).suit;
        }

        /* internal  */checkIsInThisSuit(arg1: int, arg2: int): Boolean
        {
            var loc2=null;
            var loc3=0;
            var loc1=SuitConfig.instance.getInfoByCode(arg1);
            if (loc1) 
            {
                loc2 = loc1.partList.split(",");
                loc3 = 0;
                while (loc3 < loc2.length) 
                {
                    if (parseInt(loc2[loc3]) == arg2) 
                    {
                        return true;
                    }
                    ++loc3;
                }
            }
            return false;
        }

        /* internal  */checkHasBindEquip(): Boolean
        {
            var loc1=0;
            while (loc1 < 3) 
            {
                if (allItem[loc1].itemData) 
                {
                    if (allItem[loc1].itemData.itemInfo.item.bind == EBind._EBindYes || allItem[loc1].itemData.itemExInfo.bind == EBind._EBindYes) 
                    {
                        return true;
                    }
                }
                ++loc1;
            }
            return false;
        }

        /* internal  */showResultEquip(): void
        {
            var loc5=null;
            var loc6=null;
            if (!allItem[0].itemData || !allItem[1].itemData || !allItem[2].itemData) 
            {
                allItem[3].itemData = null;
                return;
            }
            var loc1=(allItem[0].itemData.itemInfo.extend as TItemWeapon).suit;
            var loc2=allItem[2].itemData.itemInfo.item.type;
            var loc3=this.checkHasBindEquip() ? EBind._EBindYes : _EBindAfterUse;
            var loc4;
            if (loc4 = ItemConfig.instance.getEquipByParams(loc1, loc2, loc3)) 
            {
                loc5 = new ItemData(loc4.item.code);
                loc6 = new Object();
                if (loc3 == EBind._EBindYes) 
                {
                    loc6["bind"] = EBind._EBindYes;
                }
                if (typeof loc5.itemInfo.extend === "titemweapon") 
                {
                    loc6["durable"] = (loc5.itemInfo.extend as TItemWeapon).durable;
                    loc6["prefix"] = (loc5.itemInfo.extend as TItemWeapon).prefix;
                }
                loc5.setItemExInfo(loc6);
                allItem[3].itemData = loc5;
            }
            else 
            {
                allItem[3].itemData = null;
            }
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length && allItem[arg1].itemData) 
            {
                this._currentMaterialEquips.splice(this._currentMaterialEquips.indexOf(allItem[arg1].itemData), 1);
                allItem[arg1].itemData = null;
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this._currentMaterialEquips, false, true));
            }
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this._currentMaterialEquips = [];
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60197));
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60196));
            }
            return;
        }

        /* internal */ /* var */_operationId: int=10;

        /* internal */ /* var */_currentMaterialEquips: Array<any>;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_hintText1: string;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */_lastPutinIndex: int=0;
    }
